<template>
  <el-dialog title="新增" :close-on-click-modal="false" :visible.sync="visible">
    <el-steps :active="active" finish-status="success">
      <el-step title="选择分销商"></el-step>
      <el-step title="选择酒店"></el-step>
    </el-steps>
    <el-form :model="dataForm" ref="dataForm" label-width="80px">
      <!--选择分销商-->
      <el-form-item prop="dtName" label v-if="active === 0" label-width="0px">
        <el-popover
          ref="dtListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtTree"
            @current-change="dtListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.dtName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择分销商"
        ></el-input>
      </el-form-item>
    </el-form>
    <!--选择酒店-->
    <template v-if="active === 1">
      <el-tabs v-model="dataForm.authType" type="border-card">
        <el-tab-pane name="1" label="批量授权">
          <el-radio v-model="dataForm.authAll" label="1">授权所有酒店</el-radio>
          <el-radio v-model="dataForm.authAll" label="0"
            >授权指定条件酒店</el-radio
          >
          <el-form
            :inline="true"
            :model="dfHotel"
            v-if="dataForm.authAll === '0'"
          >
            <el-form-item>
              <el-select
                v-model="dfHotel.province"
                clearable
                @change="proChange"
                placeholder="请选择省份"
              >
                <el-option
                  v-for="item in provinces"
                  :key="item.cityCode"
                  :label="item.cityNameZh"
                  :value="item.cityCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="dfHotel.city"
                clearable
                @change="cityChange"
                placeholder="请选择城市"
              >
                <el-option
                  v-for="item in citys"
                  :key="item.cityCode"
                  :label="item.cityNameZh"
                  :value="item.cityCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="dfHotel.county"
                clearable
                placeholder="请选择区域"
              >
                <el-option
                  v-for="item in countys"
                  :key="item.cityCode"
                  :label="item.cityNameZh"
                  :value="item.cityCode"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="dfHotel.brand"
                clearable
                placeholder="请选择品牌"
              >
                <el-option
                  v-for="item in brands"
                  :key="item.id"
                  :label="item.bname"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane name="2" label="授权指定酒店">
          <el-row style="margin-bottom: 10px">
            <el-form :inline="true" :model="dataForm">
              <el-form-item>
                <el-input
                  v-model="dataForm.hotelName"
                  placeholder="酒店名称"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-select
                  v-model="dataForm.province"
                  clearable
                  @change="proChange"
                  placeholder="请选择省份"
                >
                  <el-option
                    v-for="item in provinces"
                    :key="item.cityCode"
                    :label="item.cityNameZh"
                    :value="item.cityCode"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-select
                  v-model="dataForm.city"
                  clearable
                  @change="proChange"
                  placeholder="请选择城市"
                >
                  <el-option
                    v-for="item in citys"
                    :key="item.cityCode"
                    :label="item.cityNameZh"
                    :value="item.cityCode"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <el-col :span="7" style="margin-left: 22px">
              <el-button type="primary" @click="queryByName">查询</el-button>
              <el-button
                type="primary"
                @click="select"
                :disabled="dataListSelections.length <= 0"
                >添加</el-button
              >
            </el-col>
          </el-row>
          <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%"
          >
            <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50"
            ></el-table-column>
            <el-table-column
              prop="hotelName"
              header-align="center"
              align="center"
              label="酒店名称"
            ></el-table-column>
            <el-table-column
              prop="address"
              header-align="center"
              align="center"
              label="酒店地址"
            ></el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper"
          ></el-pagination>
        </el-tab-pane>
      </el-tabs>
    </template>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="prev" v-if="active != 0"
        >上一步</el-button
      >
      <el-button type="primary" @click="next" v-if="active != 2"
        >下一步</el-button
      >
      <el-button
        type="primary"
        :disabled="isExcu"
        @click="dataFormSubmit"
        v-if="active === 2"
        >提交</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      isExcu: false,
      visible: false,
      active: 0,
      dfHotel: {
        province: null,
        city: null,
        county: null,
        dtId: null,
        dtName: null,
        brand: null,
        hotelName: null,
      },
      tier: 1,
      parentId: -1,
      provinces: [],
      citys: [],
      countys: [],
      dtList: [],
      brands: [],
      dataForm: {
        authType: "1",
        authAll: "1",
        dtId: "",
        dtName: "",
        hotelName: "",
        hotelIds: [],
        province: null,
        city: null,
        county: null,
      },
      dtList: [],
      dtListTreeProps: {
        label: "name",
        children: "children",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
    };
  },
  methods: {
    prev() {
      this.active--;
    },
    next() {
      // 第一步选择分销商
      if (this.active === 0 && this.dataForm.dtId === "") {
        this.$alert("请选择分销商", "提示", {
          confirmButtonText: "确定",
          callback: (action) => {},
        });
        return;
      }
      // 第二步，按照区域授权
      if (
        this.active === 1 &&
        this.dataForm.authType === "1" &&
        this.dataForm.authAll === "0"
      ) {
        if (
          this.dfHotel.province === null &&
          this.dfHotel.city === null &&
          this.dfHotel.county === null &&
          this.dfHotel.brand === null
        ) {
          this.$alert("请选择授权的区域或品牌", "提示", {
            confirmButtonText: "确定",
            callback: (action) => {},
          });
          return;
        }
      }
      // 第二步，授权指定酒店
      if (this.active === 1 && this.dataForm.authType === "2") {
        if (
          this.dataForm.hotelIds.length <= 0 &&
          this.dataListSelections.length <= 0
        ) {
          this.$alert("请选择授权的酒店", "提示", {
            confirmButtonText: "确定",
            callback: (action) => {},
          });
          return;
        }
      }
      this.active++;
    },
    // 分销商树选中
    dtListTreeCurrentChangeHandle(data, node) {
      this.dataForm.dtId = data.id;
      this.dataForm.dtName = data.name;
    },
    // 获取区域查询条件
    getQuryCitys() {
      this.$http({
        url: this.$http.adornUrl("/ht/hthotelinfo/citys"),
        method: "post",
        params: this.$http.adornParams({
          tier: this.tier,
          parentId: this.parentId,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          switch (this.tier) {
            case 1:
              this.provinces = data.list;
              break;
            case 2:
              this.citys = data.list;
              break;
            case 3:
              this.countys = data.list;
              break;
          }
        }
      });
    },
    // 选择省份
    proChange(value) {
      this.parentId = value;
      this.tier = 2;
      this.getQuryCitys();
    },
    // 选择城市
    cityChange(value) {
      this.parentId = value;
      this.tier = 3;
      this.getQuryCitys();
    },
    init(dtList, provinces, brands) {
      this.dtList = dtList;
      this.provinces = provinces;
      this.brands = brands;
      this.visible = true;
      this.active = 0;
      this.dataListSelections = [];
      this.dataForm.hotelIds = [];
    },
    // 获取指定的酒店（按酒店名称）
    getDataList() {
      this.dataListSelections = [];
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/dt/htl/list/hotel"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          dtId: this.dataForm.dtId,
          name: this.dataForm.hotelName,
          //province: this.dataForm.province,
          city: this.dataForm.city,
          county: this.dataForm.county
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    queryByName() {
      this.pageIndex = 1;
      this.getDataList();
    },
    select() {
      if (this.dataListSelections.length <= 0) {
        return;
      }
      let ids = this.dataListSelections.map((item) => {
        return item.id;
      });
      // 把选择的酒店添加到酒店列表
      this.dataForm.hotelIds = this.dataForm.hotelIds.concat(ids);
      // 去重
      this.dataForm.hotelIds = this.dataForm.hotelIds.filter((item, index) => {
        return this.dataForm.hotelIds.indexOf(item) === index;
      });
      this.dataListSelections = [];
    },
    // 表单提交
    dataFormSubmit() {
      this.isExcu = true;
      this.select();
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          let strBrand =
            this.dfHotel.brand === null || this.dfHotel.brand.length <= 0
              ? null
              : this.dfHotel.brand.reduce((pre, cur) => {
                  return pre + "," + cur;
                });
          this.$http({
            url: this.$http.adornUrl("/dt/htl/save"),
            method: "post",
            data: this.$http.adornData({
              dtId: this.dataForm.dtId,
              authType: this.dataForm.authType,
              authAll: this.dataForm.authAll,
              // 'province': this.dfHotel.province,
              city: this.dfHotel.city,
              county: this.dfHotel.county,
              brand: this.dfHotel.brand,
              hotelIds: this.dataForm.hotelIds,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: data.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.dataListSelections = [];
                  this.isExcu = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
              this.isExcu = false;
            }
            this.dataListSelections = [];
            this.dataForm.hotelIds = [];
          });
        }
      });
    },
  },
};
</script>
